package com.zxy.rabbitmqdemo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;

/**
 * Created by zxybird on 2018/3/1.
 * 
 * 如果消息没有到exchange,则confirm回调,ack=false
 * 
 * 如果消息到达exchange,则confirm回调,ack=true
 * 
 * exchange到queue成功,则不回调return
 * 
 * exchange到queue失败,则回调return(需设置mandatory=true,否则不回回调,消息就丢了)
 *
 * @see <a href="http://blog.csdn.net/qq315737546/article/details/54176560">reference</a>
 */
public class LogConfirmCallback implements RabbitTemplate.ConfirmCallback {

  private final Logger LOG = LoggerFactory.getLogger(getClass());

  @Override
  public void confirm(CorrelationData correlationData, boolean ack) {

    if (ack) {
      // ack == true, 代表已经到了Exchange了
      LOG.info("Confirm RabbitMQ message correlationData = {}, ack = {}", correlationData, ack);
    } else {
      // ack == false, 代表没有到Exchange, 需要error一下
      LOG.error("Confirm RabbitMQ message correlationData = {}, ack = {}", correlationData, ack);
    }

  }
}
